package dbAccess;


import java.io.*;
import java.util.*;
import java.util.Date;
import java.sql.*;


public class Appointment {

	private String defaultStr="unknown";
	private int appointmentId;	
	private int patientId;
	private int docId;
	private int staffId;
	private String date;
	private String startTime;
	private String endTime;
	private String building;
	private String room;
	private String statusMsg;
	private String creator;
	
	public Appointment(String creatorIn)
	{
		patientId=-1;
		docId=-1;
		staffId=-1;
		date=defaultStr;
		startTime=defaultStr;
		endTime=defaultStr;
		building=defaultStr;
		room=defaultStr;
		statusMsg="";
		creator = creatorIn;
	}
	
	public Boolean createAppointment()
	{
		DbAccess access = new DbAccess();
		//int docid = Integer.parseInt(access.ExecuteScalar(access.connect(), "SELECT DocId FROM tbl_appointment WHERE date = '" + date + "' AND StartTime <='" + startTime + "'"));
		try
		{
			ResultSet rs = access.ExecuteSelect(access.connect(), "SELECT DocId FROM tbl_appointment WHERE docId='" + docId + "' AND date = '" + date + "' AND StartTime <='" + startTime + "' AND EndTime >='" + startTime + "' and IsDeleted = 0");
			if (rs.next())
			{
				this.setStatusMsg("Doctor already has an appointment at this time. Please choose a different time");
				return false;
			}
			
			rs = access.ExecuteSelect(access.connect(), "SELECT DocId FROM tbl_appointment WHERE docId='" + docId + "' AND date = '" + date + "' AND StartTime <='" + endTime + "' AND EndTime >='" + endTime + "' and IsDeleted = 0");
			if (rs.next())
			{
				this.setStatusMsg("Doctor has another appointment coming up before this one ends. Please choose a different time");
				return false;
			}
			
			rs = access.ExecuteSelect(access.connect(), "SELECT * FROM tbl_appointment WHERE patientId='" + patientId + "' AND date = '" + date + "' AND StartTime <='" + startTime + "' AND EndTime >='" + startTime + "' and IsDeleted = 0");
			if (rs.next())
			{
				this.setStatusMsg("Patient already has an appointment at this time. Please choose a different time");
				return false;
			}
			
			rs = access.ExecuteSelect(access.connect(), "SELECT * FROM tbl_appointment WHERE patientId='" + patientId + "' AND date = '" + date + "' AND StartTime <='" + endTime + "' AND EndTime >='" + endTime + "' and IsDeleted = 0");
			if (rs.next())
			{
				this.setStatusMsg("Patient has another appointment coming up before this one ends. Please choose a different time");
				return false;
			}
			
			rs = access.ExecuteSelect(access.connect(), "SELECT * FROM tbl_appointment WHERE patientId='" + patientId + "' AND date = '" + date + "' AND StartTime <='" + startTime + "' AND EndTime >='" + startTime + "' AND Building='" + building + "' AND Room='" + room + "' and IsDeleted = 0");
			if (rs.next())
			{
				this.setStatusMsg("There is already an appointment in this room at this time. Please choose a different room");
				return false;
			}
			
			rs = access.ExecuteSelect(access.connect(), "SELECT * FROM tbl_appointment WHERE patientId='" + patientId + "' AND date = '" + date + "' AND StartTime <='" + endTime + "' AND EndTime >='" + endTime + "' AND Building='" + building + "' AND Room='" + room + "' and IsDeleted  = 0");
			if (rs.next())
			{
				this.setStatusMsg("There is an appointment starting in this room before this one ends. Please choose a different room");
				return false;
			}
		}
		catch (SQLException e)
		{
			this.setStatusMsg("Duplication check error occurred: " + e.getMessage());
			return false;
		}
		
		String query = "INSERT INTO tbl_appointment";
		query += "(DocId, PatientId, Date, StartTime, EndTime, Building, Room, StaffId, IsDeleted)";
		query += "VALUES ('" + docId + "', '" + patientId + "', '" + date + "', '" + startTime + "', '" + endTime + "', '" + building + "', '" + room + "', '" + staffId + "', 0)";
		
		if(!access.ExecuteUpdate(access.connect(), query))
		{
			this.setStatusMsg("Could not update tbl_appointment with appointment information.");
			return false;
		}
		else
		{
			Login.Log(creator,query);
		}
		
		//store patient ID field
		if (!(evalAppointmentId()))
		{
			return false;
		}
		
		this.setStatusMsg("New appointment created with ID: " + this.getAppointmentId());
		//this.setStatusMsg("New appointment created with ID: ");
		return true;
	}
	
	public int getPatientId() {
		return patientId;
	}

	public void setPatientId(int patientId) {
		this.patientId = patientId;
	}

	public int getDocId() {
		return docId;
	}

	public void setDocId(int docId) {
		this.docId = docId;
	}

	public int getStaffId() {
		return staffId;
	}

	public void setStaffId(int staffId) {
		this.staffId = staffId;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getBuilding() {
		return building;
	}

	public void setBuilding(String building) {
		this.building = building;
	}

	public String getRoom() {
		return room;
	}

	public void setRoom(String room) {
		this.room = room;
	}

	public String getStatusMsg() {
		return statusMsg;
	}

	public void setStatusMsg(String statusMsg) {
		this.statusMsg = statusMsg;
	}

	public Boolean evalAppointmentId() {
		DbAccess access = new DbAccess();
		String query = "select tbl_appointment.AppointmentId from tbl_appointment where tbl_appointment.DocId = " + this.docId + " and ";
		query += "tbl_appointment.StartTime = '" + this.startTime + "' and tbl_appointment.IsDeleted = 0";
		
		try
		{
			ResultSet rs = access.ExecuteSelect(access.connect(), query);
			rs.next();
			this.appointmentId = rs.getInt("tbl_appointment.AppointmentId");
			return true;
		}
		catch (SQLException e)
		{
			return false;
		}	
	}

	public int getAppointmentId()
	{
		return this.appointmentId;
	}
	public void setAppointmentId(int appointmentId) {
		this.appointmentId = appointmentId;
	}


}
